package com.kc.gb.device.controller;

import com.kc.gb.base.dto.Result;
import com.kc.gb.device.common.adapter.DeviceAdaptor;
import com.kc.gb.device.common.adapter.dto.basic.station.enums.LightType;
import com.kc.gb.device.common.properties.DeviceProperties;
import com.kc.gb.device.common.utils.I18NHelper;
import com.kc.gb.device.common.utils.StringPool;
import com.kc.gb.device.controller.dto.HeaderMenu;
import com.kc.gb.device.service.StripAdminService;
import com.kc.gb.device.service.support.dto.StationContainerSlotLayout;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/admin/device")
public class HtmlAdminController {

    @Resource
    DeviceAdaptor deviceAdaptor;

    @Resource
    DeviceProperties deviceProperties;

    @Resource
    StripAdminService stripAdminService;



    @GetMapping("/menus")
    @ResponseBody
    public Result<List<HeaderMenu>> menus(){
        String stationCode = StringPool.EMPTY;
        try {
            // station code 获取异常, 则展示默认菜单
            stationCode = deviceAdaptor.getApplicationStationCode();
        } catch (Exception e) {
            log.info("Station code error:{}", e.getMessage());
        }
        List<HeaderMenu> menus = new ArrayList<>();
        // 系统信息菜单
        menus.add(Menus.index.getMenu());
        if(StringUtils.isNotEmpty(stationCode)){
            // 分拨墙料格灯菜单
            menus.add(Menus.wall_ptl.getMenu());
            // 料格灯光菜单： 默认为 PTL
            LightType containerLightType = LightType.ptl;
            try {
                StationContainerSlotLayout layout = stripAdminService.getStationContainerSlotLights(deviceProperties.getWarehouseId(),stationCode);
                containerLightType = layout.getLightType();
            } catch (Exception e) {
                // station 获取异常, 则展示默认 ContainerLightType = LightType.ptl
                log.info("Station Layout error:{}", e.getMessage());
            }
            if(LightType.ptl.equals(containerLightType)){
                menus.add(Menus.container_ptl.getMenu());
            }else{
                menus.add(Menus.strip.getMenu());
            }
            // 工业相机菜单
            menus.add(Menus.camera.getMenu());
        }
        // 斜顶菜单: 斜顶只有调试模式，默认展示
        menus.add(Menus.slant.getMenu());
        // 配置菜单
        menus.add(Menus.config.getMenu());
        return Result.success(menus);
    }

    @GetMapping("/index")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("index");
        modelAndView.addObject("active","index");
        return modelAndView;
    }

    @GetMapping("/wall_ptl")
    public ModelAndView sptl(){
        ModelAndView modelAndView = new ModelAndView("wall_ptl");
        modelAndView.addObject("active","wall-ptl");
        return modelAndView;
    }

    @GetMapping("/container_ptl")
    public ModelAndView cptl(){
        ModelAndView modelAndView = new ModelAndView("container_ptl");
        modelAndView.addObject("active","container-ptl");
        return modelAndView;
    }

    @GetMapping("/slant")
    public ModelAndView slant(){
        ModelAndView modelAndView = new ModelAndView("slant");
        modelAndView.addObject("active","slant");
        return modelAndView;
    }

    @GetMapping("/strip")
    public ModelAndView strip(){
        ModelAndView modelAndView = new ModelAndView("strip");
        modelAndView.addObject("active","strip");
        return modelAndView;
    }

    @GetMapping("/camera")
    public ModelAndView camera(){
        ModelAndView modelAndView = new ModelAndView("camera");
        modelAndView.addObject("active","camera");
        return modelAndView;
    }

    @GetMapping("/config")
    public ModelAndView config(){
        ModelAndView modelAndView = new ModelAndView("config");
        modelAndView.addObject("active","config");
        return modelAndView;
    }

    @GetMapping("/test")
    public ModelAndView test(){
        ModelAndView modelAndView = new ModelAndView("test");
        modelAndView.addObject("active","test");
        return modelAndView;
    }

    public enum Menus{
        index(new HeaderMenu("index","/admin/device/index","fas fa-warehouse", "device.html.system_info")),
        wall_ptl(new HeaderMenu("wall_ptl","/admin/device/wall_ptl","fas fa-sun", "device.html.wall_ptl")),
        container_ptl(new HeaderMenu("container_ptl","/admin/device/container_ptl","fas fa-sun", "device.html.container_ptl")),
        strip(new HeaderMenu("strip","/admin/device/strip","fas fa-lightbulb", "device.html.container_strip")),
        camera(new HeaderMenu("camera","/admin/device/camera","fas fa-camera", "device.html.camera")),
        slant(new HeaderMenu("slant","/admin/device/slant","fas fa-less-than-equal", "device.html.slant")),
        config(new HeaderMenu("config","/admin/device/config","fas fa-cogs", "device.html.config")),
        ;

        private HeaderMenu menu;

        Menus(HeaderMenu menu) {
            this.menu = menu ;
        }

        public HeaderMenu getMenu() {
            return menu;
        }

    }
}
